package cn.chencq.moudle.lottery.service.impl;

import java.lang.reflect.Method;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;

import cn.chencq.moudle.lottery.dao.LotteryAnalysisDao;
import cn.chencq.moudle.lottery.entity.Lottery;
import cn.chencq.moudle.lottery.entity.LotteryAnalysis;
import cn.chencq.moudle.lottery.entity.LotteryDebt;
import cn.chencq.moudle.lottery.service.LotteryAnalysisService;
import cn.chencq.moudle.lottery.service.LotteryService;
import cn.chencq.moudle.sys.dao.BaseDao;
import cn.chencq.moudle.sys.service.impl.BaseServiceImpl;

@Service
public class LotteryAnalysisServiceImpl extends BaseServiceImpl<LotteryAnalysis> implements LotteryAnalysisService {

	private static final Logger logger = LoggerFactory.getLogger(LotteryAnalysisServiceImpl.class);

	@Autowired
	private LotteryAnalysisDao lotteryAnalysisDao;

	@Autowired
	private LotteryService lotteryService;

	@Override
	protected BaseDao getBaseDao() {
		return this.lotteryAnalysisDao;
	}

	@Override
	public void saveOrUpdate(LotteryAnalysis lotteryAnalysis) {
		lotteryAnalysisDao.saveOrUpdate(lotteryAnalysis);
	}

	@Override
	public LotteryAnalysis groupCount(String lotteryDate, String lotteryType) {
		List<Lottery> lotterys = lotteryService.queryByDate(lotteryDate, lotteryType);
		LotteryAnalysis la = new LotteryAnalysis(lotteryDate, lotteryType);

		for (Lottery lottery : lotterys) {
			String lotteryContent = lottery.getLotteryContent();
			List<String> list = JSONArray.parseArray(lotteryContent, String.class);
			for (String t : list) {
				if (StringUtils.isNotEmpty(t)) {
					parse(t, la);
				}
			}
		}
		return la;
	}

	private void parse(String t, LotteryAnalysis la) {
		if (StringUtils.isNumeric(t)) {
			if (t.startsWith("0") && t.length() > 1) {
				t = t.substring(1);
			}

			Class<?> cls = la.getClass();
			try {
				Method getN = cls.getMethod("getN" + t);
				int num = (getN.invoke(la) == null ? 0 : (int) getN.invoke(la));

				Method m2 = cls.getMethod("setN" + t, Integer.class);
				m2.invoke(la, num + 1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if ("大".equals(t)) {
			int da = la.getDa() == null ? 0 : la.getDa();
			la.setDa(da + 1);
		}

		if ("小".equals(t)) {
			int xiao = la.getXiao() == null ? 0 : la.getXiao();
			la.setXiao(xiao + 1);
		}

		if ("单".equals(t)) {
			int dan = la.getDan() == null ? 0 : la.getDan();
			la.setDan(dan + 1);
		}

		if ("双".equals(t)) {
			int shuang = la.getShuang() == null ? 0 : la.getShuang();
			la.setShuang(shuang + 1);
		}

		if ("大单".equals(t)) {
			int daDan = la.getDaDan() == null ? 0 : la.getDaDan();
			la.setDaDan(daDan + 1);
		}

		if ("大双".equals(t)) {
			int daShuang = la.getDaShuang() == null ? 0 : la.getDaShuang();
			la.setDaShuang(daShuang + 1);
		}

		if ("小单".equals(t)) {
			int xiaoDan = la.getXiaoDan() == null ? 0 : la.getXiaoDan();
			la.setXiaoDan(xiaoDan + 1);
		}

		if ("小双".equals(t)) {
			int xiaoShuang = la.getXiaoShuang() == null ? 0 : la.getXiaoShuang();
			la.setXiaoShuang(xiaoShuang + 1);
		}

		if ("极小".equals(t)) {
			int jiXiao = la.getJiXiao() == null ? 0 : la.getJiXiao();
			la.setJiXiao(jiXiao + 1);
		}

		if ("极大".equals(t)) {
			int jiDa = la.getJiDa() == null ? 0 : la.getJiDa();
			la.setJiDa(jiDa + 1);
		}
	}

	@Override
	public double followAlgorithm(String lotteryDate, String lotteryType) {
		double[] arr = { 1, 2, 3, 5, 7, 10, 15, 20, 25, 35, 50 };
		// 1.按照期次从小到大的开奖结果集合
		List<Lottery> list = lotteryService.queryByDate(lotteryDate, lotteryType);
		if (null == list || list.isEmpty()) {
			return 0;
		}

		// 2.
		int index = 0;
		double totalDebt = 0.0;
		double totalWin = 0.0;
		for (int i = 1; i < list.size(); i++) {
			index++;
			LotteryDebt ld = new LotteryDebt();
			ld.setLotteryId(list.get(i).getLotteryId());
			ld.setLotteryType(lotteryType);
			int currPlayType = parseLottery(list.get(i-1));
			ld.setCurrPlayType(currPlayType);
			int winPlayType = parseLottery(list.get(i));
			ld.setWinPlayType(winPlayType);
			ld.setIndex(index);
			ld.setDebt(arr[index - 1]);
			totalDebt += arr[index - 1];

			if (index >= 11) {
				index = 0;
			} else {
				if (winPlayType == currPlayType) {
					index = 0;
					totalWin += ld.getDebt() * 3.9;
				}
			}
			logger.info("follow: " + ld.toString());
		}

		return (totalWin - totalDebt);
	}

	/**
	 * 
	 * @Title: parseLottery
	 * @Description: 1 大单， 大双 2, 小单 3, 小双 4
	 * @param l
	 * @return int
	 */
	public int parseLottery(Lottery l) {
		int r = 0;
		int sum = 0;
		String[] array = l.getLotteryNumbers().split(",");
		for (int i = 0; i < array.length; i++) {
			sum += Integer.parseInt(array[i]);
		}
		if (sum > 14) {
			if (sum % 2 == 0) {
				r = 2;
			} else {
				r = 1;
			}
		} else {
			if (sum % 2 == 0) {
				r = 4;
			} else {
				r = 3;
			}
		}

		return r;
	}

}
